package com.example.drainage.ui.certupload.dataview

import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 * 输入框
 *
 * @author 创建人 ：yanghaozhang
 * @version 1.0
 * @package 包名 ：com.augurit.agmobile.agwater5.drainage.complex.emergency.dangerous.view.dataview
 * @createTime 创建时间 ：2021/10/31
 * @modifyBy 修改人 ：
 * @modifyTime 修改时间 ：
 * @modifyMemo 修改备注：
 */
open class SingleSelectRecyclerDataSpot(
    outputKey: String,
    private val recyclerView: RecyclerView,
    private val adapter: SingleSelectRecyclerAdapter,
    title: String
) : BaseViewDataSpot<DataSpotBean, Pair<String, DataSpotBean>>(
    outputKey,
    recyclerView,
    title
) {

    private var dataList = mutableListOf<DataSpotBean>()

    // 外面输入的临时的值
    private var tempSelectValue: DataSpotBean? = null

    // 外面输入的临时的值
    private var tempSelectText: String? = null

    var selectFirstDefault = false

    init {
        recyclerView.adapter = adapter
    }

    override fun getToastErrorMsg(): String = "请选择${dataTitle}"

    override fun isReady(): Boolean {
        if (dataIsRequired && getDataType() == FormState.STATE_EDIT) {
            return recyclerView.isVisible && adapter.selectIndex in dataList.indices
        }
        return true
    }

    /**
     * 设置纯值
     */
    override fun setPureData(data: DataSpotBean) {
        tempSelectValue = data
        updateSelectValue()
    }

    fun setDataByTextSearch(data: String) {
        tempSelectText = data
        updateSelectValue()
    }

    /**
     * 获取纯值
     */
    override fun getPureData(): DataSpotBean? {
        return dataList.getOrNull(adapter.selectIndex)
    }

    /**
     * 设置包装过的值
     */
    override fun setPackData(data: Pair<String, DataSpotBean>) {
        dataOutputKey = data.first
        setPureData(data.second)
    }

    fun setDataByTextPairSearch(data: Pair<String, String>) {
        dataOutputKey = data.first
        setDataByTextSearch(data.second)
    }

    /**
     * 获取包装过的值
     */
    override fun getPackData(): Pair<String, DataSpotBean>? {
        return dataList.getOrNull(adapter.selectIndex)?.let {
            dataOutputKey to it
        }
    }

    // ---------------------自定义方法--------------------

    private fun updateSelectValue() {
        if (dataList.isEmpty()) {
            return
        }
        // 是否选中了一个Item
        var hasSelectValue = false
        tempSelectValue?.let { temp ->
            dataList.indexOfFirst {
                it.text == temp.text
            }.takeIf {
                it != -1
            }?.also {
                changeSelect(it)
                hasSelectValue = true
            }
        }
        tempSelectText?.takeIf {
            !hasSelectValue
        }.let { temp ->
            dataList.indexOfFirst {
                it.text == temp
            }.takeIf {
                it != -1
            }?.also {
                changeSelect(it)
                hasSelectValue = true
            }
        }
        if (!hasSelectValue && selectFirstDefault) {
            changeSelect(0)
            hasSelectValue = true
        }

        updateViewAfterSelect()

        tempSelectValue = null
        tempSelectText = null
    }

    private fun updateViewAfterSelect() {
        adapter.notifyDataSetChanged()
    }

    private fun changeSelect(index: Int) {
        adapter.selectIndex = index
    }

    fun setDataSelectList(list: List<DataSpotBean>) {
        dataList.clear()
        dataList.addAll(list)
        adapter.setNewData(dataList)
        changeSelect(-1)
        updateSelectValue()
    }

    abstract class SingleSelectRecyclerAdapter(layoutId: Int) : BaseQuickAdapter<DataSpotBean, BaseViewHolder>(layoutId) {
        var selectIndex = -1
    }
}